<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: whoson.php,v 1.15 2007/01/30 21:02:43 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");

require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'update_body.tpl'));

$migration  = array(    0   => array(
                                "INSERT INTO ${dbprefix}config (config_name, config_value) VALUES ('DB_VERSION',0)"
                            ),
                        1   => array(
                                "ALTER TABLE `${dbprefix}sessions` ADD `session_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `session_id`",
                                "UPDATE ${dbprefix}config SET config_value=1 WHERE config_name='DB_VERSION'"
                            ),
                   );

if (!isset($config['DB_VERSION'])) {
    $db_version = -1;
} else {
    $db_version = $config['DB_VERSION'];
}

$updates    = false;
foreach ($migration as $version => $sql_array) {
    if ($db_version < $version) {
        foreach ($sql_array as $sql) {
            $updates    = true;
            $result     = $db2->sql_query($sql);
            if (!$result) {
                $err    = $db2->sql_error();
                $result = "<B>Error #{$err['code']}:</b> {$err['message']}";
            } else {
                $rows   = $db2->sql_affectedrows($result);
                $result = "OK, $rows rows affected";
            }
            $template->assign_block_vars('update', array(
                'QUERY'     => $sql,
                'RESULT'    => $result
            ));
        }
    }
}

if (!$updates) {
    $template->assign_block_vars('switch_no_updates', array());
}
$template->pparse('body');

